package com.unionftech.common.permissions.tip.impl.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.unionftech.common.R;
import com.unionftech.common.permissions.tip.bean.DialogStyleInfo;
import com.unionftech.common.permissions.tip.bean.PermissionTipInfo;

import java.util.List;

public class PermissionsDescribeDialog extends DialogFragment {
    private final Context mContext;
    private final List<PermissionTipInfo> mList;
    private OnDismissListener mDismissListener;
    private final DialogStyleInfo mDialogStyleInfo;

    public PermissionsDescribeDialog(Context context, List<PermissionTipInfo> list, DialogStyleInfo dialogStyleInfo) {
        this.mContext = context;
        this.mList = list;
        this.mDialogStyleInfo = dialogStyleInfo;
    }

    @Override
    public void onStart() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            }
        }
        super.onStart();
    }

    public void setDismissListener(OnDismissListener dismissListener) {
        this.mDismissListener = dismissListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setGravity(Gravity.CENTER);
                window.getDecorView().setPadding(0, 0, 0, 0);
            }
        }
        return LayoutInflater.from(mContext).inflate(mDialogStyleInfo.getDialogLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = view.findViewById(R.id.dialog_rv);
        recyclerView.setAdapter(new TipDescribeAdapter(mList));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        view.findViewById(R.id.tip_id_recycle_view).setOnClickListener(v -> {
            this.dismiss();
            if (mDismissListener != null) {
                mDismissListener.onAction();
            }
        });
    }

    class TipDescribeAdapter extends RecyclerView.Adapter<TipDescribeViewHolder> {
        private List<PermissionTipInfo> mList;

        public TipDescribeAdapter(List<PermissionTipInfo> list) {
            this.mList = list;
        }

        @NonNull
        @Override
        public TipDescribeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(mDialogStyleInfo.getItemLayoutId(), parent, false);
            return new TipDescribeViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull TipDescribeViewHolder holder, int position) {
            PermissionTipInfo info = mList.get(position);
            holder.permissionsTitle.setText(info.getTitle());
            holder.permissionsTip.setText(info.getTip());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    static class TipDescribeViewHolder extends RecyclerView.ViewHolder {
        private TextView permissionsTitle;
        private TextView permissionsTip;

        public TipDescribeViewHolder(@NonNull View itemView) {
            super(itemView);
            permissionsTitle = itemView.findViewById(R.id.tip_id_item_title);
            permissionsTip = itemView.findViewById(R.id.tip_id_item_content);
        }
    }
}
